import openai
from openai.types.chat import ChatCompletionSystemMessageParam, ChatCompletionUserMessageParam

# 硅基流动的sk
OPENAI_API_KEY = "sk-zfusfsppautcgeerkonwgssluqqbbpqqdhddoutookjegawy"
client = openai.OpenAI(api_key=OPENAI_API_KEY, base_url="https://api.siliconflow.cn")

MODEL_NAME = "deepseek-ai/DeepSeek-V3"


def chat_with_model_block():
    # 调用模型并获取完整响应
    response = client.chat.completions.create(
        model=MODEL_NAME,
        messages=[
            ChatCompletionSystemMessageParam(role="system", content="You are a helpful assistant."),
            ChatCompletionUserMessageParam(role="user", content="什么是大模型？")
        ],
        temperature=0.9,
    )
    print(response.choices[0].message.content)


def chat_with_model_stream():
    # 使用openai的complete接口完成一次对话，并使用流式返回结果
    for chunk in client.chat.completions.create(
            model=MODEL_NAME,
            messages=[
                ChatCompletionSystemMessageParam(role="system", content="You are a helpful assistant."),
                ChatCompletionUserMessageParam(role="user", content="什么是大模型？")
            ],
            stream=True,
            temperature=0.9,
    ):
        print(chunk.choices[0].delta.content or "", end="")


if __name__ == '__main__':
    # chat_with_model_block()
    chat_with_model_stream()
